﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Xml;

using Palsta.Core;

namespace InfoPathTranslate
{
    public partial class Form1 : Form
    {

        private ImportFileDialog dlg = null;
        private string path = "";
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 读取数据文件
        /// </summary>
        /// <param name="pFileName"></param>
        /// <returns></returns>
        public string Import(string pFileName)
        {
            if (!File.Exists(pFileName))
            {
                MessageBox.Show("读取XSN数据文件不存在！");
                return null;
            }
            string dataDir = SysConfig.GetTempDirPath();
            try
            {
                //解压目录
                Compress.WinrarUnZipFile(pFileName, dataDir);
            }
            catch (Exception ex)
            {
                MessageBox.Show("读取XSN数据文件已损坏或格式错误！");
                return null;
            }

            return dataDir;
        }

        private void tsbOpen_Click(object sender, EventArgs e)
        {
            if (dlg == null)
            {
                this.dlg = new ImportFileDialog();
            }
            DialogResult rst = dlg.ShowDialog(this);
            if (rst == DialogResult.OK)
            {
                path = dlg.FileName;
                if (path.Length == 0)
                {
                    return;
                }
                String upzipPath = this.Import(path);
                if (upzipPath == null)
                {
                    return;
                }
                InfoPathTranslater translater = new InfoPathTranslater();
                String outputDir = path.Replace(".xsn", "_html").Replace(' ', '_');
                translater.Translate(upzipPath, outputDir);
                String htmlFile = outputDir + "\\view.html";
                Uri url = new Uri(htmlFile);
                webKitBrowser1.Navigate(url.AbsoluteUri);
                webKitBrowser1.DocumentCompleted += GetValue;
            }
        }

        private void GetValue(object sender, EventArgs e)
        {
            //WebKit.DOM.Element txt = webKitBrowser1.Document.GetElementById("Text01");
            //MessageBox.Show(txt.GetAttribute("value"));
            //txt.SetAttribute("value", "123");
        }


        private void tsbAdd_Click(object sender, EventArgs e)
        {

        }

        private void tsbSave_Click(object sender, EventArgs e)
        {
            //xsd验证xml
            //设置xml路径
            String xmlPath =  "\\template.xml";
            //设置xsd路径
            String xsdPath = "\\myschema.xsd";
            try
            {
                using (XmlTextReader txtreader = new XmlTextReader(xmlPath))
                {
                    using (XmlValidatingReader oreader = new XmlValidatingReader(txtreader))
                    {
                        oreader.Schemas.Add(null, xsdPath);
                        while (oreader.Read())
                        {

                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void tsbExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    
    }
}
